﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

using NHibernate;

using ProjectBase.Core;
using ProjectBase.Data;
using ProjectBase.Data.NH;

using Survey.Common.Data.Services;
using Survey.Entity.Services;

namespace Survey.Data.Services
{
    public class TranslatorDao : BaseDao<Translator, int, TranslatorFilter>, ITranslatorDao
    {
        /// <summary>
        ///     String indicating the path Phrases.Localization
        /// </summary>
        const string Localization = "Phrases.Localization";

        #region ITranslatorDao Members
        /// <summary>
        ///     Evaluates all filters and order-by clauses
        ///     and returns sorted list of matching Translator entities
        /// </summary>
        /// <param name = "filter">ISearchObject&lt;Translator&gt; containing TranslatorFilter not-null instance</param>
        /// <returns>List of Translator entities matching the searching criteria</returns>
        public override IEnumerable<Translator> Find(TranslatorFilter filter)
        {
            Criteria.AddCompare(() => filter.IdExact); // Searches for exact ID

            Criteria.AddEqual(() => Entity.Key, filter.KeySearch); // even 'string.Empty' is used for WHERE clause
            Criteria.AddEqual(() => Entity.Area, filter.AreaSearch); // even 'string.Empty' is used for WHERE clause

            Criteria.AddLike(() => filter.Key); // only not empty nor null is used for WHERE clause
            Criteria.AddLike(() => filter.Area); // only not empty nor null is used for WHERE clause


            ICriteria phrasesCriteria = null;

            if (filter.SearchedPhrase.IsNotEmpty()) // filter for phrase?
            {
                phrasesCriteria = Criteria.CreateSubCriteria(() => Entity.Phrases);
                phrasesCriteria.AddLike(() => Entity.Phrases[0].Localization, filter.SearchedPhrase);
                // searched language
                phrasesCriteria.AddLike(() => Entity.Phrases[0].Language, filter.SearchedLanguageName);
            }

            var orderByPhrase = string.Empty;
            var orderByValue = true;
            if (filter.OrderBy.IsNotEmpty()) // if there is OrderBy
            {
                // let's try to provide
                var orderBy = filter.OrderBy.First(); // order by on phrases
                if (orderBy.Key.Contains('.')) // if "Phrases.cs"  or "Phrases.en-GB"
                {
                    if (phrasesCriteria.IsNull()) // if language filter was not added
                    {
                        // the ordered language is used
                        var language = orderBy.Key.Split('.')[1];
                        phrasesCriteria = Criteria.CreateSubCriteria(() => Entity.Phrases);
                        // else multi records will be returned
                        phrasesCriteria.AddLike(() => Entity.Phrases[0].Language, language);

                    } // new order
                    orderByPhrase = orderBy.Key;
                    orderByValue = orderBy.Value;
                    filter.OrderBy = new Dictionary<string, bool> { { Localization, orderByValue } };

                } // result will be filtered by asked language
            } // and sorted by phrases in that language

            var result = GetListByCriteria(filter);
            if (orderByPhrase.IsNotEmpty())
            {
                filter.OrderBy = new Dictionary<string, bool> { { orderByPhrase, orderByValue } };
            }
            return result;
        }

        public virtual IEnumerable<Translator> GetAll()
        {
            var list = Session.CreateCriteria<Translator>().List<Translator>();
            return list;
        }
        #endregion
    }
}